﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Workflow.Project
{
    public partial class Log : System.Web.UI.Page
    {
        public PJLog result = null;
        private PJLogManager objPJLogManager = new PJLogManager();
        private WorkflowManager objWorkflowManager = new WorkflowManager();
        private UserTeamRoleManager objUserTeamRoleManager = new UserTeamRoleManager();
        public int workParentId;
        public int fromPage;
        public int pjCount;
        public int statusNo;
        protected void Page_Load(object sender, EventArgs e)
        {
            workParentId = Convert.ToInt32(Request.QueryString["WorkParentId"]);
            fromPage = Convert.ToInt32(Request.QueryString["FromPage"]);
            if(!IsPostBack)
            {
                try
                {
                    pjCount = objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 6, 19);
                    statusNo = objWorkflowManager.GetWorkflowStatues(workParentId);
                    result = objPJLogManager.GetLog(workParentId);
                    if(result!=null)
                    {
                        txtPJNo.Text = result.PJId;
                        ddlStage.SelectedItem.Text = result.Stage;
                        ddlNode.SelectedItem.Text = result.Node;
                        txtProblem.Text = result.Problem;
                        txtSource.Text = result.Source;
                        txtPerson.Text = result.Person;
                        ddlDepartment.SelectedItem.Text = result.Department;
                        ddlStatues1.Text = result.Statues1;
                        ddlDegree.Text = result.Degree;
                        txtPs.Text = result.Ps;
                        txtStatues.Text = result.Statues;
                        if (fromPage == 0 || statusNo != 100)
                        {
                            if (pjCount > 0 && statusNo < 1) //项目
                            {
                                Update.Visible = true;
                                EnabledControl();
                            }
                            if (pjCount > 0 && statusNo == 2) //项目
                            {
                                UpdateStatues.Visible = true;
                            }
                        }
                        if (statusNo == 100) //已完成
                        {
                            UpdateStatues.Visible = true;
                        }
                    }

                    else
                    {
                        if (statusNo == 0)
                        {
                            txtPJNo.Text = "";
                            txtProblem.Text = "";
                            txtSource.Text = "";
                            txtPerson.Text = "";
                            txtPs.Text = "";
                            txtStatues.Text = "";
                            Create.Visible = true;
                            txtPJNo.Enabled = true;
                            EnabledControl();
                        }
                    }
                }
                catch (Exception ex)
                {
                    string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                }
            }
            
        }

        protected void Create_Click(object sender, EventArgs e)
        {
            PJLog objPJLog = Info();
            try
            {
                objPJLogManager.InsertLog(objPJLog);
                string msg = string.Format("<script>alert('保存成功！')</script>");
                string str = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }

        protected void Update_Click(object sender, EventArgs e)
        {
            PJLog objPJLog = Info();
            try
            {
                objPJLogManager.UpdateLog(objPJLog);
                string msg = string.Format("<script>alert('保存成功！')</script>");
                string str = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }
        protected void UpdateStatues_Click(object sender, EventArgs e)
        {
            try
            {
                objPJLogManager.UpdateStatues(workParentId);
                string msg = string.Format("<script>alert('同步成功！')</script>");
                string str = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }
        private PJLog Info()
        {
            PJLog objPJLog = new PJLog
            {
                WorkParentId = workParentId,
                PJId = txtPJNo.Text.Trim().ToUpper(),
                Stage = ddlStage.SelectedValue,
                Node = ddlNode.SelectedValue,
                Problem = txtProblem.Text.Trim(),
                Source = txtSource.Text.Trim(),
                Person = txtPerson.Text.Trim(),
                Department = ddlDepartment.SelectedValue,
                Statues1 = ddlStatues1.SelectedValue,
                Degree = ddlDegree.SelectedValue,
                StartDate = Request.Params.Get("txtStartDate") == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(Request.Params.Get("txtStartDate")),
                CompleteDate = Request.Params.Get("txtCompleteDate") == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(Request.Params.Get("txtCompleteDate")),
                RealCompleteDate = Request.Params.Get("txtRealCompleteDate") == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(Request.Params.Get("txtRealCompleteDate")),
                Ps = txtPs.Text.Trim()
            };
            return objPJLog;
        }
        void EnabledControl()
        {
            ddlStage.Enabled = true;
            ddlNode.Enabled = true;
            txtProblem.Enabled = true;
            txtSource.Enabled = true;
            txtPerson.Enabled = true;
            ddlDepartment.Enabled = true;
            ddlStatues1.Enabled = true;
            ddlDegree.Enabled = true;
            txtPs.Enabled = true;
        }

    }
}